home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / misc / dialoglib.lha / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-07  |  6.1 KB  |  245 lines

  1. #include <libraries/gadtools.h>
  2. #include <graphics/text.h>
  3. #include <proto/diskfont.h>
  4. #include <proto/gadtools.h>
  5. #include <proto/graphics.h>
  6. #include <proto/intuition.h>
  7. #include <proto/utility.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include "dialog.h"
  11. #ifdef DEBUG1
  12.     #include <stdio.h>
  13. #endif
  14.  
  15. static ULONG getDisplayStructure( DialogElement *de )
  16. {
  17.     ULONG place, structure = DESF_HBaseline;
  18.  
  19.     if( !de )
  20.         return 0;
  21.  
  22.     if( GetTagData( NGDA_GadgetText, 0, de->taglist ) )
  23.     {
  24.         place = getTextPlacement( GetTagData( NGDA_Flags, 0, de->taglist ), PLACETEXT_LEFT );
  25.         switch( place )
  26.         {
  27.         case PLACETEXT_LEFT:
  28.         case PLACETEXT_RIGHT:
  29.             structure |= DESF_VBaseline;
  30.             break;
  31.         }
  32.     }
  33.     return structure;
  34. }
  35.  
  36. static VOID setupDisplay( DialogElement *de, ULONG kind )
  37. {
  38.     struct TextExtent te;
  39.     struct RastPort rp;
  40.     struct TextAttr *ta;
  41.     struct TextFont *tf;
  42.     STRPTR text;
  43.     ULONG place;
  44.     LONG minwidth, mintop, minbottom, textwidth, texttop, textbottom;
  45.  
  46.     if( !de )
  47.         return;
  48.  
  49.     switch( kind )
  50.     {
  51.     case TEXT_KIND:
  52.         de->idcmp_mask |= TEXTIDCMP;
  53.         break;
  54.     case NUMBER_KIND:
  55.         de->idcmp_mask |= NUMBERIDCMP;
  56.         break;
  57.     }
  58.     de->idcmp_mask |= IDCMP_REFRESHWINDOW;
  59.  
  60.     ta = (struct TextAttr *)GetTagData( NGDA_TextAttr, 0, de->taglist );
  61.     if( !ta )
  62.         return;
  63.     tf = OpenDiskFont( ta );
  64.     if( !tf )
  65.         return;
  66.  
  67.     minwidth = 8 + 3 * tf->tf_XSize;
  68.     mintop = 2 + tf->tf_Baseline;
  69.     minbottom = 2 + tf->tf_YSize - tf->tf_Baseline;
  70.  
  71.     text = (STRPTR)GetTagData( NGDA_GadgetText, 0, de->taglist );
  72.     place = getTextPlacement( GetTagData( NGDA_Flags, 0, de->taglist ), PLACETEXT_LEFT );
  73.  
  74.     if( text )
  75.     {
  76.         InitRastPort( &rp );
  77.         SetFont( &rp, tf );
  78.         TextExtent( &rp, text, strlen( text ), &te );
  79.     }
  80.     else
  81.     {
  82.         te.te_Extent.MinX = te.te_Extent.MinY = 0;
  83.         te.te_Extent.MaxX = te.te_Extent.MaxY = -1;
  84.     }
  85.  
  86.     CloseFont( tf );
  87.  
  88. #ifdef DEBUG1
  89.     printf( "setupDisplay : min x %d, max x %d, min y %d, max y %d\n",
  90.         te.te_Extent.MinX, te.te_Extent.MaxX, te.te_Extent.MinY, te.te_Extent.MaxY );
  91. #endif
  92.  
  93.     textwidth = te.te_Extent.MaxX + 1 - te.te_Extent.MinX;
  94.     texttop = - te.te_Extent.MinY;
  95.     textbottom = te.te_Extent.MaxY + 1;
  96.  
  97.     switch( place )
  98.     {
  99.     case PLACETEXT_ABOVE:
  100.         if( minwidth < textwidth )
  101.             minwidth = textwidth;
  102.         setMinWidth( de, minwidth );
  103.         setMaxWidth( de, MAX_SPACE );
  104.         setMinTopExtent( de, texttop + textbottom + INTERHEIGHT );
  105.         setMaxTopExtent( de, texttop + textbottom + INTERHEIGHT );
  106.         setMinBottomExtent( de, mintop + minbottom );
  107.         setMaxBottomExtent( de, mintop + minbottom );
  108.         break;
  109.     case PLACETEXT_BELOW:
  110.         if( minwidth < textwidth )
  111.             minwidth = textwidth;
  112.         setMinWidth( de, minwidth );
  113.         setMaxWidth( de, MAX_SPACE );
  114.         setMinTopExtent( de, mintop + minbottom );
  115.         setMaxTopExtent( de, mintop + minbottom );
  116.         setMinBottomExtent( de, texttop + textbottom + INTERHEIGHT );
  117.         setMaxBottomExtent( de, texttop + textbottom + INTERHEIGHT );
  118.         break;
  119.     case PLACETEXT_LEFT:
  120.         if( mintop < texttop )
  121.             mintop = texttop;
  122.         if( minbottom < textbottom )
  123.             minbottom = textbottom;
  124.         setMinLeftExtent( de, textwidth + INTERWIDTH  );
  125.         setMaxLeftExtent( de, textwidth + INTERWIDTH  );
  126.         setMinRightExtent( de, minwidth );
  127.         setMaxRightExtent( de, MAX_SPACE );
  128.         setMinTopExtent( de, mintop );
  129.         setMaxTopExtent( de, mintop );
  130.         setMinBottomExtent( de, minbottom );
  131.         setMaxBottomExtent( de, minbottom );
  132.         break;
  133.     case PLACETEXT_RIGHT:
  134.         if( mintop < texttop )
  135.             mintop = texttop;
  136.         if( minbottom < textbottom )
  137.             minbottom = textbottom;
  138.         setMinLeftExtent( de, minwidth );
  139.         setMaxLeftExtent( de, MAX_SPACE );
  140.         setMinRightExtent( de, textwidth + INTERWIDTH );
  141.         setMaxRightExtent( de, textwidth + INTERWIDTH );
  142.         setMinTopExtent( de, mintop );
  143.         setMaxTopExtent( de, mintop );
  144.         setMinBottomExtent( de, minbottom );
  145.         setMaxBottomExtent( de, minbottom );
  146.         break;
  147.     case PLACETEXT_IN:
  148.         if( minwidth < textwidth )
  149.             minwidth = textwidth;
  150.         if( mintop < texttop )
  151.             mintop = texttop;
  152.         if( minbottom < textbottom )
  153.             minbottom = textbottom;
  154.         setMinWidth( de, minwidth );
  155.         setMaxWidth( de, MAX_SPACE );
  156.         setMinTopExtent( de, mintop );
  157.         setMaxTopExtent( de, mintop );
  158.         setMinBottomExtent( de, minbottom );
  159.         setMaxBottomExtent( de, minbottom );
  160.         break;
  161.     }
  162. }
  163.  
  164. static ULONG layoutDisplay( DialogElement *de, LayoutMessage *lm, ULONG kind )
  165. {
  166.     struct NewGadget ng;
  167.     ULONG error = DIALOGERR_OK;
  168.  
  169.     if( !de )
  170.         return DIALOGERR_BAD_ARGS;
  171.     if( !lm )
  172.         return DIALOGERR_BAD_ARGS;
  173.  
  174. #ifdef DEBUG1
  175.     printf(
  176.     "layoutDisplay : x %d, y %d, width %d, height %d, left %d, right %d, top %d, bottom %d\n",
  177.         lm->lm_X, lm->lm_Y, lm->lm_Width, lm->lm_Height,
  178.         lm->lm_Left, lm->lm_Right, lm->lm_Top, lm->lm_Bottom );
  179. #endif
  180.     ng.ng_GadgetText = (UBYTE *)GetTagData( NGDA_GadgetText, 0, de->taglist );
  181.     ng.ng_TextAttr = (struct TextAttr *)GetTagData( NGDA_TextAttr, 0, de->taglist );
  182.     ng.ng_VisualInfo = (APTR)GetTagData( NGDA_VisualInfo, 0, de->taglist );
  183.     ng.ng_Flags = GetTagData( NGDA_Flags, 0, de->taglist );
  184.     layoutGTSingleLined( &ng, lm, PLACETEXT_LEFT );
  185.     de->object = CreateGadgetA( kind, *lm->lm_PreviousPtr, &ng, de->taglist );
  186.     *lm->lm_PreviousPtr = de->object;    /* advance "previous" pointer to new object */
  187.     if( !de->object )
  188.         error = DIALOGERR_NO_MEMORY;
  189.     return error;
  190. }
  191.  
  192. ULONG dispatchText( struct Hook *hook, DialogElement *de, DialogMessage *dm )
  193. {
  194.     ULONG result;
  195.  
  196.     switch( dm->dm_MethodID )
  197.     {
  198.     case DIALOGM_GETSTRUCT:
  199.         result = getDisplayStructure( de );
  200.         break;
  201.     case DIALOGM_SETUP:
  202.         setupDisplay( de, TEXT_KIND );
  203.         break;
  204.     case DIALOGM_LAYOUT:
  205.         result = layoutDisplay( de, (LayoutMessage *)dm, TEXT_KIND );
  206.         break;
  207.     case DIALOGM_MATCH:
  208.         result = 0;
  209.         break;
  210.     case DIALOGM_CLEAR:
  211.         break;
  212.     case DIALOGM_SETATTRS:
  213.         setGTAttrs( de, (SetAttrsMessage *)dm );
  214.         break;
  215.     }
  216.     return result;
  217. }
  218.  
  219. ULONG dispatchNumber( struct Hook *hook, DialogElement *de, DialogMessage *dm )
  220. {
  221.     ULONG result;
  222.  
  223.     switch( dm->dm_MethodID )
  224.     {
  225.     case DIALOGM_GETSTRUCT:
  226.         result = getDisplayStructure( de );
  227.         break;
  228.     case DIALOGM_SETUP:
  229.         setupDisplay( de, NUMBER_KIND );
  230.         break;
  231.     case DIALOGM_LAYOUT:
  232.         result = layoutDisplay( de, (LayoutMessage *)dm, NUMBER_KIND );
  233.         break;
  234.     case DIALOGM_MATCH:
  235.         result = 0;
  236.         break;
  237.     case DIALOGM_CLEAR:
  238.         break;
  239.     case DIALOGM_SETATTRS:
  240.         setGTAttrs( de, (SetAttrsMessage *)dm );
  241.         break;
  242.     }
  243.     return result;
  244. }
  245.